﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Base;

namespace UtilZ.Dotnet.Ex.Communication.Net.MQBase
{
    /// <summary>
    /// MQ映射项
    /// </summary>
    public class MQMapItem
    {
        private static readonly ReadOnlyCollection<Type> _valueSupportTypes;

        static MQMapItem()
        {
            var typeList = new List<System.Type> {
                TypeEx.BoolType,
                TypeEx.ByteAarrayType,
                TypeEx.CharType,
                TypeEx.DoubleType,
                TypeEx.FloatType,
                TypeEx.ByteType,
                TypeEx.Int16Type,
                TypeEx.Int32Type,
                TypeEx.Int64Type,
                TypeEx.SByteType,
                TypeEx.UInt16Type,
                TypeEx.UInt32Type,
                TypeEx.UInt64Type,
                TypeEx.StringType
            };
            _valueSupportTypes = new ReadOnlyCollection<System.Type>(typeList);
        }


        /// <summary>
        /// Key
        /// </summary>
        public string Key { get; private set; }

        /// <summary>
        /// Value类型
        /// </summary>
        internal Type Type { get; private set; }

        /// <summary>
        /// Value
        /// </summary>
        public object Value { get; private set; }



        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        public MQMapItem(string key, object value)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            System.Type type = value.GetType();
            if (!_valueSupportTypes.Contains(type))
            {
                throw new ArgumentException($"不支持的值类型:{type.FullName}");
            }

            this.Type = type;
            this.Key = key;
            this.Value = value;
        }
    }
}
